/*
 * FTPApplet.java
 *
 * Created on Jan 9, 2011, 10:12:05 AM
 *
 * @author pquiring
 */

import java.awt.*;
import javax.swing.*;

import javaforce.*;

public class FTPApplet extends javax.swing.JApplet {

  /** Initializes the applet FTPApplet */
  public void init() {
    try {
      java.awt.EventQueue.invokeAndWait(new Runnable() {
        public void run() {
          initComponents();
          Site.loadIcons(this.getClass());
        }
      });
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    tabs = new javax.swing.JTabbedPane();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jSeparator1 = new javax.swing.JPopupMenu.Separator();
    jMenuItem2 = new javax.swing.JMenuItem();
    jMenuItem3 = new javax.swing.JMenuItem();
    jSeparator3 = new javax.swing.JPopupMenu.Separator();
    jMenuItem6 = new javax.swing.JMenuItem();
    jMenu2 = new javax.swing.JMenu();
    jMenuItem4 = new javax.swing.JMenuItem();

    jMenu1.setMnemonic('F');
    jMenu1.setText("File");

    jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
    jMenuItem1.setMnemonic('S');
    jMenuItem1.setText("Site Manager");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem1ActionPerformed(evt);
      }
    });
    jMenu1.add(jMenuItem1);
    jMenu1.add(jSeparator1);

    jMenuItem2.setMnemonic('I');
    jMenuItem2.setText("Import Sites...");
    jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem2ActionPerformed(evt);
      }
    });
    jMenu1.add(jMenuItem2);

    jMenuItem3.setMnemonic('E');
    jMenuItem3.setText("Export Sites...");
    jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem3ActionPerformed(evt);
      }
    });
    jMenu1.add(jMenuItem3);
    jMenu1.add(jSeparator3);

    jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
    jMenuItem6.setMnemonic('C');
    jMenuItem6.setText("Close Site");
    jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem6ActionPerformed(evt);
      }
    });
    jMenu1.add(jMenuItem6);

    jMenuBar1.add(jMenu1);

    jMenu2.setMnemonic('H');
    jMenu2.setText("Help");

    jMenuItem4.setMnemonic('A');
    jMenuItem4.setText("About");
    jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem4ActionPerformed(evt);
      }
    });
    jMenu2.add(jMenuItem4);

    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(tabs, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(tabs, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
    );
  }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
      SiteDetails sdArray[] = SiteMgr.showSiteMgr(null);
      if (sdArray == null) return;
      for(SiteDetails sd : sdArray) connect(sd);
}//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
      Settings.importSettings();
}//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
      Settings.exportSettings();
}//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
      closeSite();
}//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
      JFAWT.showMessage("About", "jfftp/" + FTPApp.version + "\nFTP/SMB Client\nWebsite:http://jfftp.sourceforge.net\nBy:Peter Quiring(pquiring@gmail.com)");
}//GEN-LAST:event_jMenuItem4ActionPerformed


  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JMenu jMenu1;
  private javax.swing.JMenu jMenu2;
  private javax.swing.JMenuBar jMenuBar1;
  private javax.swing.JMenuItem jMenuItem1;
  private javax.swing.JMenuItem jMenuItem2;
  private javax.swing.JMenuItem jMenuItem3;
  private javax.swing.JMenuItem jMenuItem4;
  private javax.swing.JMenuItem jMenuItem6;
  private javax.swing.JPopupMenu.Separator jSeparator1;
  private javax.swing.JPopupMenu.Separator jSeparator3;
  private javax.swing.JTabbedPane tabs;
  // End of variables declaration//GEN-END:variables

  public void connect(SiteDetails sd) {
    Site site = null;
    if (sd.protocol.equals("ftp")) site= new SiteFTP();
    if (sd.protocol.equals("ftps")) site= new SiteFTPS();
    if (sd.protocol.equals("sftp")) site= new SiteSFTP();
    if (sd.protocol.equals("smb")) site= new SiteSMB();
    if (site == null) return;
    site.init(sd.localDir);
    JScrollPane pane = new JScrollPane(site);
    pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    JPanel panel = new JPanel(new GridLayout());
    panel.putClientProperty("site", site);
    site.putClientProperty("panel", panel);
    site.putClientProperty("pane", pane);
    panel.add(pane);
    tabs.addTab(sd.name, panel);
    tabs.setSelectedComponent(panel);
    if (!site.connect(sd)) return;
    site.requestFocus();
  }
  public void closeSite() {
    try {
      JPanel panel = (JPanel)tabs.getSelectedComponent();
      if (panel == null) return;
      Site site = (Site)panel.getClientProperty("site");
      if (site == null) return;
      site.disconnect();
      tabs.remove(panel);
    } catch (Exception e) {
      JFLog.log(e);
    }
  }
}
